package com.flexdms.jsfutils;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 * Display Zero as empty String
 * @author jason
 *
 */
@FacesConverter(ZeroLongConverter.CONVERTER_ID)
public class ZeroLongConverter implements Converter
{

	public static final String CONVERTER_ID="zerolong";
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value)
	{
		if (value==null)
			return new Long(0);
		else if (value.toString().trim().length()==0)
		{
			return new Long(0);
		} else
		{
			return Long.parseLong(value.toString().trim());
		}
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value)
	{
		if (value==null)
			return "";
		
		else if (value instanceof Double)
		{
			Double d=(Double)value;
			if (d==0d)
			{
				return "";
			}
			
		} else if (value instanceof Long)
		{
			Long i=(Long)value;
			if (i==0l)
			{
				return "";
			}
			
		} else if (value instanceof Integer)
		{
			Integer i=(Integer)value;
			if (i==0)
			{
				return "";
			}
		}
		return value.toString();
	}

}
